﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.Forms
{
    public partial class SimplifiedProcess : MetroFramework.Forms.MetroForm
    {
        public SimplifiedProcess()
        {
            InitializeComponent();
            this.Text = "检测规程";
            this.MinimizeBox = true;
            this.MaximizeBox = false;
            ShowVerificationProcess();
            treeVerificationProcess.NodeClick += TreeVerificationProcess_NodeClick;

            okReturnToolbar21.Event_OKClick += OkReturnToolbar21_Event_OKClick;
            okReturnToolbar21.Event_ReturnClick += OkReturnToolbar21_Event_ReturnClick;            
        }

        private void OkReturnToolbar21_Event_ReturnClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void OkReturnToolbar21_Event_OKClick(object sender, EventArgs e)
        {
            try
            {
                RelationsLMP.Model.SimplifiedProcess.SimplifiedProcessInfo simplifiedProcessInfo = SimplifiedProcessItem.SimplifiedProcessInfo;
                if (simplifiedProcessInfo.IndicationErrorInfo.gasCiruitTypeList.Count <= 0)
                    throw new Exception("请选择【示值误差】对应的气路信息！");
                RelationsLMP.Common.Methods.SaveSimplifiedProcessInfo(RelationsLMP.Common.StaticInfo.GasVerificationProcessSimplifiedInfo, RelationsLMP.Common.StaticInfo.GasVerificationProcessSimplifiedFilePath);

                string[] paras = new string[2];
                string fileBase64 = Relations.Common.Methods.FileToBase64String(RelationsLMP.Common.StaticInfo.GasVerificationProcessSimplifiedFilePath);
                paras[0] = Static.ParaInfo.CurrentUser.tonken;
                paras[1] = "{\"fileBase64\":\"" + fileBase64 + "\"}";
                object result = Relations.Common.WebServiceHelper.InvokeWebService(Static.ParaInfo.webServiceURL, "uploadVerificationProcess", paras);
                string errorInfo;
                if (!Static.Methods.webServiceCheckResultIsRight(result.ToString(), out errorInfo))
                    throw new Exception(errorInfo);
                MessageBox.Show(this, "保存成功！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this,"保存失败，错误信息：" + ex.Message,"异常信息",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        List<Model.DB.tb_ParaInfo> paraInfo;
        public List<Model.DB.tb_ParaInfo> ParaInfoList
        {
            get { return paraInfo; }
            set { paraInfo = value; }
        }

        UC.SimplifiedProcessItem SimplifiedProcessItem;

        private void TreeVerificationProcess_NodeClick(object sender, DevComponents.AdvTree.TreeNodeMouseEventArgs e)
        {
            if (e.Node.Level != 1) return;
            if (SimplifiedProcessItem == null)
            {
                SimplifiedProcessItem = new UC.SimplifiedProcessItem();
                SimplifiedProcessItem.Dock = DockStyle.Fill;
            }
            RelationsLMP.Model.SimplifiedProcess.SimplifiedProcessInfo simplifiedProcessInfo = (RelationsLMP.Model.SimplifiedProcess.SimplifiedProcessInfo)e.Node.Tag;
            simplifiedProcessInfo = RelationsLMP.Common.StaticInfo.GasVerificationProcessSimplifiedInfo.Find(value => value.GasName.Id == simplifiedProcessInfo.GasName.Id);
            SimplifiedProcessItem.Init(simplifiedProcessInfo, paraInfo);
            if (pSimplifiedProcessItem.Controls.Count <= 0)
                pSimplifiedProcessItem.Controls.Add(SimplifiedProcessItem);
        }

        private void ShowVerificationProcess()
        {
            DevComponents.AdvTree.Node _ParentNode, _ChildNode;
            treeVerificationProcess.Nodes.Clear();
            _ParentNode = new DevComponents.AdvTree.Node();
            _ParentNode.Text = "气体类型";
            foreach (RelationsLMP.Model.SimplifiedProcess.SimplifiedProcessInfo _SimplifiedProcessInfo in RelationsLMP.Common.StaticInfo.GasVerificationProcessSimplifiedInfo)
            {
                _ChildNode = new DevComponents.AdvTree.Node();
                _ChildNode.Text = _SimplifiedProcessInfo.GasName.Description;
                _ChildNode.Tag = _SimplifiedProcessInfo;
                _ParentNode.Nodes.Add(_ChildNode);
                if (treeVerificationProcess.SelectedNode == null)
                    treeVerificationProcess.SelectedNode = _ChildNode;
            }
            treeVerificationProcess.Nodes.Add(_ParentNode);
            treeVerificationProcess.ExpandAll();
            if (treeVerificationProcess.SelectedNode != null)
                TreeVerificationProcess_NodeClick(null, new DevComponents.AdvTree.TreeNodeMouseEventArgs(treeVerificationProcess.SelectedNode,MouseButtons.Left,1,1,1,1));
        }
    }
}
